package com.jifen.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SystemUtils {

    /**
     * 清除HTML内容
     * @param content
     * @return
     */
    public static String clearHtmlTag(String content){
        String regex = "<[^>]+>";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        content = matcher.replaceAll("");
        content = content.replaceAll("&nbsp;", "");
        content = content.replaceAll("//s*|/t|/r|\r|/n|\n", "");
        return content;
    }

    /**
     * 获取用户IP地址
     * @param request
     * @return 用户IP
     */
    public static String getRemoteAddr(HttpServletRequest request) {
        String remoteAddr = request.getHeader("X-Forwarded-For");
        String ip = remoteAddr != null ? remoteAddr : request.getRemoteAddr();
        System.out.println("ipipip:"+ip);
        if(StringUtils.isNotBlank(ip) && ip.contains(",")){
            ip = ip.substring(0, ip.indexOf(","));
        }
/*        if (StringUtils.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("X-Real-IP");
        } else if (StringUtils.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("Proxy-Client-IP");
        } else if (StringUtils.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("WL-Proxy-Client-IP");
        }*/
        return ip;
    }
}
